namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganBuildBLL
    {
        public int Add(OrganBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganBuild(");
            builder.Append("BuildName)");
            builder.Append(" values (");
            builder.Append("@BuildName)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.BuildName;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int BuildID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganBuild ");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuildID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int BuildID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrganBuild");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuildID;
            return DBHelper.Exists(builder.ToString(), cmdParms);
        }

        public List<OrganBuild> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            builder.Append(" BuildID,BuildName ");
            builder.Append(" FROM OrganBuild ");
            DataSet set = DBHelper.Query(builder.ToString());
            List<OrganBuild> list = null;
            if (set.Tables[0].Rows.Count > 0)
            {
                list = new List<OrganBuild>();
                OrganBuild item = null;
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    item = new OrganBuild {
                        BuildID = Convert.ToInt32(row["BuildID"]),
                        BuildName = row["BuildName"].ToString()
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganBuild GetModel(int BuildID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 BuildID,BuildName from OrganBuild ");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuildID;
            OrganBuild build = new OrganBuild();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if ((set.Tables[0].Rows[0]["BuildID"] != null) && (set.Tables[0].Rows[0]["BuildID"].ToString() != ""))
                {
                    build.BuildID = int.Parse(set.Tables[0].Rows[0]["BuildID"].ToString());
                }
                if ((set.Tables[0].Rows[0]["BuildName"] != null) && (set.Tables[0].Rows[0]["BuildName"].ToString() != ""))
                {
                    build.BuildName = set.Tables[0].Rows[0]["BuildName"].ToString();
                }
                return build;
            }
            return null;
        }

        public bool Update(OrganBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganBuild set ");
            builder.Append("BuildName=@BuildName");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildName", SqlDbType.NVarChar, 50), new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.BuildName;
            cmdParms[1].Value = model.BuildID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

